package com.navi.loaddata.job;

import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDMapper;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices2;
import com.navi.loaddata.loadService.impl.Dcdata.LoadDcDataService;
import com.navi.loaddata.loadService.impl.Dcdata.array.LoadDcDataServiceARRAY;
import com.navi.loaddata.loadService.impl.Dcdata.cell1.LoadDcDataServiceCELL1;
import com.navi.loaddata.loadService.impl.Dcdata.cell2.LoadDcDataServiceCELL2;
import com.navi.loaddata.loadService.impl.Dcdata.cf.LoadDcDataServiceCF;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author jay
 */
@Service
//@DisallowConcurrentExecution//禁止并发
public class LoadDcData2 implements Job {
    private static final Logger logger = LoggerFactory.getLogger(LoadDcData2.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private LoadDcDataService loadDcDataService;
    @Autowired
    private IEtlConfServices2 etlConfServices;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //get JOB
        EtlConfD etlConfD = etlConfServices.getNewConf(context);
        if (null == etlConfD) return;

        etlConfD = etlConfServices.beginJob(etlConfD);
        if (StringUtils.isEmpty(etlConfD.getQueryMinute())) return;
        startETLByOpes(etlConfD);
    }

    private void startETLByOpes(EtlConfD etlConfD) {
        //获取站点
        List<String> opes = etlConfServices.getOpesList(etlConfD);
        if(opes.size()==0) opes.add(null);
        EtlConfDModel etlConfDModel = EtlConfDMapper.INSTANCE.etlConfDToModel(etlConfD);
        //循环OPE
        for (String ope : opes) {
            etlConfDModel.setOpe(ope);//跑单站点
            String[] resMsg = new String[3];//okCnt,errCnt,errMsg
            try {
                resMsg = loadDcDataService.loadDcDataMain(etlConfDModel);
                EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByOpe) return;
                etlConfDByOpe.setOpe(ope);
                etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);
            } catch (ETLException2 e) {
                String err = e.getErrMsg();
                //print Log
                logger.error("Job {} [{}]-[{}] OPE={} FAIL TO SYNCHRONIZE.\n{}\n{}",
                        etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                        etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(), ope,
                        e.getErrorCode().getErrorMsg(),LoggerUtil.stackTraceToString(e)
                );
                EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByOpe) return;
                etlConfDByOpe.setOpe(ope);
                resMsg[0] = "0";
                resMsg[1] = "-1";
                resMsg[2] = "ope=" + ope + ": " + err;
                etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);

                if (99999 == e.getErrorCode().getErrorCode()) {//无法连接,SLEEP
                    etlConfServices.resetJob(etlConfD);
                    return;
                }
            } catch (Exception e) {
                String err = LoggerUtil.stackTraceToString(e);
                logger.error("Job {} [{}]-[{}] OPE={} FAIL TO SYNCHRONIZE.\n{}",
                        etlConfD.getProjectName() + "_" + etlConfD.getJobGroupName() + "_" + etlConfD.getJobName(),
                        etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp(), ope,
                        LoggerUtil.stackTraceToString(e)
                );
                EtlConfDModel etlConfDByOpe = etlConfServices.endJobOfModel(etlConfD);
                if (null == etlConfDByOpe) return;
                etlConfDByOpe.setOpe(ope);
                resMsg[0] = "0";
                resMsg[1] = "-1";
                resMsg[2] = "ope=" + ope + ": " + err;
                etlConfServices.addHisOfModel(etlConfDByOpe, resMsg);

                if (null != e && "Y".equals(etlConfServices.specialErr(etlConfDByOpe.getJobName(),err))) {//无法连接,SLEEP
                    etlConfServices.resetJob(etlConfD);
                    return;
                }
            }
        }
        etlConfServices.endJob(etlConfD);

    }
}
